# coding=utf-8
__author__ = 'loubobooo'

from pyecharts.charts import Pie
from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.charts import Map
import json
from collections import Counter

#  获取评论中所有评分
rates = []
country = {}
with open('collection.log', mode='r', encoding='utf-8') as f:
    rows = f.readlines()
    for row in rows:
        r = row.rstrip()
        text = json.loads(r)
        douban_rate = text.get('douban_rate')
        rates.append(douban_rate)
        countries_list = text.get('countries')
        for i in range(len(countries_list)):
            a = country.get(countries_list[i]['name'])
            if a == None:
                country[countries_list[i]['name']] = 1
            else:
                country[countries_list[i]['name']] = a + 1

# 定义星级
star_attr = ['9-10', '8-9', '7-8', '6-7', '5-6', '4-5', '3-4', '2-3', '1-2', '0-1']
star_value = [
    rates.count('9.9') + rates.count('9.8') + rates.count('9.7') + rates.count('9.6') + rates.count(
        '9.5') + rates.count('9.4') + rates.count('9.3') + rates.count('9.2') + rates.count('9.1') + rates.count(
        '9.0') + rates.count('9'),
    rates.count('8.9') + rates.count('8.8') + rates.count('8.7') + rates.count('8.6') + rates.count(
        '8.5') + rates.count('8.4') + rates.count('8.3') + rates.count('8.2') + rates.count('8.1') + rates.count(
        '8.0') + rates.count('8'),
    rates.count('7.9') + rates.count('7.8') + rates.count('7.7') + rates.count('7.6') + rates.count(
        '7.5') + rates.count('7.4') + rates.count('7.3') + rates.count('7.2') + rates.count('7.1') + rates.count(
        '7.0') + rates.count('7'),
    rates.count('6.9') + rates.count('6.8') + rates.count('6.7') + rates.count('6.6') + rates.count(
        '6.5') + rates.count('6.4') + rates.count('6.3') + rates.count('6.2') + rates.count('6.1') + rates.count(
        '6.0') + rates.count('6'),
    rates.count('5.9') + rates.count('5.8') + rates.count('5.7') + rates.count('5.6') + rates.count(
        '5.5') + rates.count('5.4') + rates.count('5.3') + rates.count('5.2') + rates.count('5.1') + rates.count(
        '5.0') + rates.count('5'),
    rates.count('4.9') + rates.count('4.8') + rates.count('4.7') + rates.count('4.6') + rates.count(
        '4.5') + rates.count('4.4') + rates.count('4.3') + rates.count('4.2') + rates.count('4.1') + rates.count(
        '4.0') + rates.count('4'),
    rates.count('3.9') + rates.count('3.8') + rates.count('3.7') + rates.count('3.6') + rates.count(
        '3.5') + rates.count('3.4') + rates.count('3.3') + rates.count('3.2') + rates.count('3.1') + rates.count(
        '3.0') + rates.count('3'),
    rates.count('2.9') + rates.count('2.8') + rates.count('2.7') + rates.count('2.6') + rates.count(
        '2.5') + rates.count('2.4') + rates.count('2.3') + rates.count('2.2') + rates.count('2.1') + rates.count(
        '2.0') + rates.count('2'),
    rates.count('1.9') + rates.count('1.8') + rates.count('1.7') + rates.count('1.6') + rates.count(
        '1.5') + rates.count('1.4') + rates.count('1.3') + rates.count('1.2') + rates.count('1.1') + rates.count(
        '1.0') + rates.count('1'),
    rates.count('0.9') + rates.count('0.8') + rates.count('0.7') + rates.count('0.6') + rates.count(
        '0.5') + rates.count('0.4') + rates.count('0.3') + rates.count('0.2') + rates.count('0.1') + rates.count(
        '0.0') + rates.count('0')
]

pie = (
    Pie()
        .add("", [list(z) for z in zip(star_attr, star_value)],
             radius=["30%", "75%"],
             center=["75%", "50%"],
             rosetype="radius",
             label_opts=opts.LabelOpts(is_show=False),
             )
        # .add("", [list(z) for z in zip(star_attr, star_value)],
        #      radius=["30%", "75%"],
        #      center=["75%", "50%"],
        #      rosetype="area",
        #      label_opts=opts.LabelOpts(is_show=False),
        #      )
        .set_global_opts(
        title_opts=opts.TitleOpts(title="2019年所看电影评分星级", subtitle="数据来源：豆瓣-loubobooo采集"),
        legend_opts=opts.LegendOpts(
            orient="vertical", pos_top="15%", pos_left="2%"
        ),
    )
        .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
)
pie.render('2019年所看电影评分星级-饼图.html')

data_top20 = Counter(rates).most_common(20)  # 返回出现次数最多的20条

bar = (
    Bar(init_opts=opts.InitOpts(page_title="条形图"))
        .add_xaxis(star_attr)
        .add_yaxis("分数", data_top20)
        .set_global_opts(
        title_opts=opts.TitleOpts(title="2019年所看电影评分星级-排行TOP20"),
        datazoom_opts=[opts.DataZoomOpts(orient="vertical")]
    )
)


# 获取列表的第二个元素
def takeSecond(elem):
    return elem[1]


# 指定第二个元素排序
data_top20.sort(key=takeSecond)

attr = []
value = []
for i in range(len(data_top20)):
    attr.append(data_top20[i][0])
    value.append(data_top20[i][1])

bar.add_xaxis(value)
bar.add_yaxis('分数', attr)
bar.set_global_opts(
    title_opts=opts.TitleOpts(title="2019年所看电影评分星级-排行TOP20", subtitle="数据来源：豆瓣-loubobooo采集"),
    datazoom_opts=[opts.DataZoomOpts(orient="vertical")]
)
bar.render('2019年所看电影评分星级-排行-柱状图.html')

map = (
    Map()
        .add("数量", [list(z) for z in zip(country.keys(), country.values())], "world")
        .set_series_opts(label_opts=opts.LabelOpts(is_show=False))
        .set_global_opts(
        title_opts=opts.TitleOpts(title="2019年电影制片国家分布"),
        visualmap_opts=opts.VisualMapOpts(max_=200),
    )
)

map.render("2019年电影制片国家分布.html")
